package es.cazadoresmadridejos.beans.controller.payment;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.payment.PaymentManager;
import es.cazadoresmadridejos.beans.manager.session.UserPreferences;
import es.cazadoresmadridejos.beans.validator.payment.PaymentValidator;
import es.cazadoresmadridejos.model.Payment;
import es.cazadoresmadridejos.objs.form.PaymentForm;
import es.cazadoresmadridejos.propertyeditor.PaymentPropertyEditor;
import es.cazadoresmadridejos.util.constants.Constants;
import es.cazadoresmadridejos.util.validation.ValidationContainer;


@Controller
public class PaymentCreateModifyController {

	@Autowired private PaymentManager paymentManager;
	@Autowired private PaymentValidator paymentValidator;
	@Autowired private MessageSource messageSource;
	@Autowired(required=false) private UserPreferences userPreferences;
	
	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Throwable{
		binder.registerCustomEditor(Payment.class, new PaymentPropertyEditor(paymentManager));
	}
	
	@RequestMapping(value="/paymentCreateModify.html",method=RequestMethod.GET)
	public ModelAndView createModify(
			@RequestParam(value="id",required=false) Payment payment) throws Throwable {
		ModelAndView mv = new ModelAndView();
		if(payment == null){
			payment = new Payment();
		}
		
		Long idReason = userPreferences.getReason().getId();
		
		List<Payment> p1s = paymentManager.findAllByReason(idReason);
		List<Payment> p2s =	paymentManager.findAllNotIncludedByReason(idReason);
		
		p1s.addAll(p2s);
		PaymentForm form = new PaymentForm();
		form.getPayments().addAll(p1s);
		
		mv.addObject("paymentForm",form);
		
		return mv;
	}
	@RequestMapping(value="/paymentCreateModify.html",method=RequestMethod.POST)
	public ModelAndView submit(
			@ModelAttribute("paymentForm") PaymentForm payment, 
			BindingResult result, 
			SessionStatus status,
			HttpServletRequest request
			) throws Throwable {
		
		ModelAndView mv = new ModelAndView("redirect:index.html");
		List<ValidationContainer> errors = new ArrayList<ValidationContainer>();
		for (Payment pay : payment.getPayments()) {
			errors.addAll(paymentValidator.validate(pay));
			
		}
		if(errors.isEmpty()){
			paymentManager.saveAll(payment.getPayments());
			
			mv.addObject("okMessage", "THE_PAYMENTS_WAS_MODIFY_SUCCESSFULLY");
			mv.addObject(Constants.SCREEN, "paymentCreateModify.html");
		}else{
			mv.addObject("errors",errors);
		}
		
		return mv;
	}
}
